#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <thread.h>

using namespace std;


int main()
{
    
}



/*
void handler(int signo)
{
    cout << "child quit, father get a signal: " << signo << endl;
}

void ClearUpChild(int signo)
{
    while (true) // 一直回收
    {
        if (signo == SIGCHLD)
        {
            // 非阻塞等待 第二十六次等待失败直接退出
            pid_t rid = waitpid(-1, nullptr, WNOHANG); // 回收任意一个子进程
            if (rid > 0)
            {
                cout << "wait child success: " << rid << endl;
            }
            else if(rid <= 0)
            {
                break;
            }
        }
        cout << "wait sub process done..." << endl;
    }
}

int main()
{
    signal(SIGCHLD, ClearUpChild); // 自定义捕捉
    pid_t id = fork();
    if (id == 0)
    {
        // child
        int cnt = 5;
        while (cnt--)
        {
            cout << "I am child process: " << getpid() << endl;
            sleep(1);
        }
        cout << "child peocess died" << endl;
        exit(0); // 子进程退出
    }
    while (true)
    {
        sleep(1);
    }
}*/

/*
void Print(sigset_t pending)
{
    cout << "curr process pending: ";
    for(int sig = 31; sig > 0; sig--)
    {
        if(sigismember(&pending, sig))
            cout << "1 ";
        else
            cout << "0 ";
    }
    cout << endl;
}

void handler(int signo)
{
    cout << "signal: " << signo << endl;
    // 不断获取当前进程的pending信号集合并打印
    sigset_t pending;
    sigisemptyset(&pending);
    while(true)
    {
        sigpending(&pending);
        Print(pending);
        sleep(1);
    }
}

int main()
{
    struct sigaction act, oact;
    act.sa_handler = handler;
    act.sa_flags = 0;
    sigemptyset(&act.sa_mask);//
    // 处理2号信号时，屏蔽3、4、5号信号
    sigaddset(&act.sa_mask, 3);
    sigaddset(&act.sa_mask, 4);
    sigaddset(&act.sa_mask, 5);

    sigaction(2,&act,&oact);//对2号信号进行捕捉

    while(true)
    {
        sleep(1);
    }
    return 0;
}
*/
